package com.zzl.bonds.ticket.service.common.impl;

import com.zzl.bonds.ticket.constant.status.HandStatusEnum;
import com.zzl.bonds.ticket.constant.status.TicketStatusEnum;
import com.zzl.bonds.ticket.constant.type.OperateButtonEnum;
import com.zzl.bonds.ticket.dto.KeyValue;
import com.zzl.bonds.ticket.dto.ticketopen.TicketOpenCheckPage;
import com.zzl.bonds.ticket.service.common.TicketOpenHandStatusService;
import com.zzl.bonds.ticket.untils.TicketUtils;
import com.zzl.general.common.exception.SysException;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author : spp
 * @date : 2022/2/18 13:30
 */
@Service
public class TicketOpenHandStatusServiceImpl implements TicketOpenHandStatusService {
    @Override
    public void setOpenTicketHandleSituation(List<?> ticketList) throws SysException {

        if (CollectionUtils.isEmpty(ticketList)) {
            return;
        }
        for (Object ticket : ticketList) {
            this.setOpenOneTicketHandleSituation(ticket);
        }
    }


    @Override
    public void setOpenTicketCheckOperates(List<TicketOpenCheckPage> ticketCheckList) {
        for (TicketOpenCheckPage checkPage : ticketCheckList) {
            checkPage.setOperateButton(Collections.singletonList(new KeyValue(OperateButtonEnum.CHECK.getCode(), OperateButtonEnum.CHECK.getValue())));
        }
    }


    /**
     * 设置开立申请表的 处理状态 tooltip 颜色样式等
     *
     * @param ticket 票价dto
     * @throws SysException 业务异常
     */
    private void setOpenOneTicketHandleSituation(Object ticket) throws SysException {
        TicketStatusEnum ticketStatusEnum = TicketUtils.getTicketStatus(ticket);
        if (ObjectUtils.isEmpty(ticketStatusEnum)) {
            throw new SysException("开单列表获取票据状态异常");
        }
        Integer ticketStatus = ticketStatusEnum.getCode();
        if (TicketStatusEnum.REJECT_LIST.contains(ticketStatus)) {
            TicketUtils.setHandStatus(ticket, HandStatusEnum.FAIL.getName());
            TicketUtils.setHandStatusInfo(ticket, TicketStatusEnum.getStatusHandInfo(ticketStatus));
        } else if (!TicketStatusEnum.NORMAL_HOLDING.getCode().equals(ticketStatus)) {
            TicketUtils.setHandStatus(ticket, HandStatusEnum.OPEN_ING.getName());
            TicketUtils.setHandStatusInfo(ticket, TicketStatusEnum.getStatusHandInfo(ticketStatus));
        } else {
            TicketUtils.setHandStatus(ticket, HandStatusEnum.SUCCESS.getName());
            TicketUtils.setHandStatusInfo(ticket, TicketStatusEnum.getStatusHandInfo(ticketStatus));
        }
        TicketUtils.setHandProgress(ticket, TicketStatusEnum.getProgress(ticketStatus));
    }

    /**
     * 设置开立 的操作按钮
     *
     * @param ticketList 票对象啊
     */
    @Override
    public void setOpenTicketOperates(List<?> ticketList) throws SysException {
        for (Object ticket : ticketList) {
            List<KeyValue> keyValues = new ArrayList<>();
            KeyValue keyValue = new KeyValue(OperateButtonEnum.DETAILS.getCode(), OperateButtonEnum.DETAILS.getValue());
            Integer code = TicketUtils.getTicketStatus(ticket).getCode();
            if (TicketStatusEnum.OPEN_PENDING_REVIEW.getCode().equals(code)) {
                keyValues.add(new KeyValue(OperateButtonEnum.CANCEL.getCode(), OperateButtonEnum.CANCEL.getValue()));
            } else if (TicketStatusEnum.REJECT_LIST.contains(code)) {
                keyValues.add(new KeyValue(OperateButtonEnum.REAPPLY.getCode(), OperateButtonEnum.REAPPLY.getValue()));
            }
            keyValues.add(keyValue);
            TicketUtils.setOperateButton(ticket, keyValues);
        }
    }
}
